function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}


function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}

function radomCode(min, max) {
	return Math.floor(Math.random() * (max - min)) + min;
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};

/**
 * 判断是否为空
 */
function validatenull(val) {
	if (val instanceof Array) {
		if (val.length == 0) return true;
	} else if (val instanceof Object) {
		if (JSON.stringify(val) === '{}') return true;
	} else {
		if (val == 'null' || val == null || val == 'undefined' || val == undefined || val == '') return true;
		return false;
	}
	return false;

}

function getPage(query) {

}

function navigateLogin() {

}

function calculate(count, index) {

}

function setStore(params) {
	const {
		name,
		content,
		type,
		datetime
	} = params
	const obj = {
		dataType: typeof(content),
		content: content,
		type: type,
		datetime: new Date().getTime()
	}
//	console.log("name:" +name+ ";"+JSON.stringify(obj))
	uni.setStorageSync(name, JSON.stringify(obj));
}
	function removeState()
	{
		uni.removeStorageSync("$sc_state")
	}
	
	function removeStore(name) {
		uni.removeStorageSync(name);
	}
	
	function getStore(params) {
		const {
		name,
		type
	  } = params
	  let obj = {}
	  let content
//		console.log("name1:" +name)
		obj = uni.getStorageSync(name);
		if (validatenull(obj)) {
			return null;
		} 
		
		obj = JSON.parse(obj);
	//	console.log("name2:" +name)

		if (obj.dataType === 'string') {
			content = obj.content
		} else if (obj.dataType === 'number') {
			content = Number(obj.content)
		} else if (obj.dataType === 'boolean') {
			content = eval(obj.content)
		} else if (obj.dataType === 'object') {
			content = obj.content
		}
		return content
		
	}

	function setState(data) {
		
		return setStore({
		name: "$sc_state",
		content: data,
		type: 'uni'
		});
	}


	function getPkgRandomId() {

		const val = uni.getStorageSync('$pkgId');
		if (val) {
			return val;
		}
		var re = "skynetcloud" + getRandom(0, 7);
		var randNum = "";
		for (var i = 0; i < 10; i++) {
			randNum += Math.floor(Math.random() * 10);
		}
		re = re + randNum;
		console.log("setStore"+"$pkgId")
		//setStore("$pkgId", re);
		setStore({
		name: "$pkgId",
		content: re,
		type: 'uni'
		})
		return re;
	}



	function getRandom(min, max) {

		var r = Math.random() * (max - min);
		var re = Math.round(r + min);
		re = Math.max(Math.min(re, max), min)
		if (re == 4)
			re = re + 1;
		return re;
	}

	function getState() {
//		console.log("$sc_state")
//		console.log("setStore:"+"$sc_state")
		return getStore({
		  name: "$sc_state"
		});
	}



	function getAccess() {
		
			
		return uni.getStorageSync("$accessToken")
	}

	function setInfo(data) {
		console.log("setStore:"+"USER_KEY")
		return setStore("$USER_KEY", data);
	}

	function getInfo() {
		return getStore("$USER_KEY", data);
	}

	module.exports = {
		formatTime: formatTime,
		formatLocation: formatLocation,
		dateUtils: dateUtils,
		getPage: getPage,
		navigateLogin: navigateLogin,
		calculate: calculate,
		setStore: setStore,
		getStore: getStore,
		removeStore: removeStore,
		setState: setState,
		getState: getState,
		removeState: removeState,
		setInfo: setInfo,
		getInfo: getInfo,
		getAccess: getAccess,
		getPkgRandomId: getPkgRandomId,
		radomCode: radomCode
	}
